using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Xml;

namespace MSODev.WSS.EditFldEx
{
    public class EditFldExPage : WebAdminPageBase
    {
        protected DropDownList DDLList;
        protected DropDownList DDLField;
        protected TextBox TxtTitle;
        protected TextBox TxtAuthInfo;
        protected RadioButtonList RBLShowDisp;
        protected RadioButtonList RBLShowEdit;
        protected RadioButtonList RBLShowListSettings;
        protected RadioButtonList RBLShowNew;
        protected RadioButtonList RBLShowVersionHistory;
        protected RadioButtonList RBLShowViewForms;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!Page.IsPostBack)
            {
                BindListDropDown();
            }
        }

        protected void DDLList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResetFieldControls();
            DDLField.Items.Clear();

            SPList list = GetList();
            if (list == null) return;
            DDLField.Items.Add(string.Empty);
            SPFieldCollection fields = list.Fields;
            List<ListItem> items = new List<ListItem>(fields.Count + 1);
            foreach (SPField field in fields)
                items.Add(new ListItem(string.Format("{0} {1}", field.Title, field.AuthoringInfo), field.Id.ToString()));
            items.Sort(delegate(ListItem x, ListItem y) { return x.Text.CompareTo(y.Text); });
            foreach (ListItem item in items) DDLField.Items.Add(item);
        }

        protected void DDLField_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResetFieldControls();

            SPField field = GetField();
            BindFieldControls(field);
        }

        protected void BtnCancel_Click(object sender, EventArgs e)
        {
            SPField field = GetField();
            BindFieldControls(field);
        }

        protected void BtnOK_Click(object sender, EventArgs e)
        {
            SPField field = GetField();

            // Update the Authoring Info attribute
            XmlDocument updatedSchema = new XmlDocument();
            updatedSchema.LoadXml(field.SchemaXml);            
            XmlAttribute authoringInfo = updatedSchema.CreateAttribute("AuthoringInfo");
            authoringInfo.Value = TxtAuthInfo.Text;                        
            updatedSchema.FirstChild.Attributes.Append(authoringInfo);
            field.SchemaXml = updatedSchema.OuterXml;            

            // Update the rest of the field's properties
            field.Title = TxtTitle.Text;
            field.ShowInDisplayForm = GetRBLValue(RBLShowDisp);
            field.ShowInEditForm = GetRBLValue(RBLShowEdit);
            field.ShowInListSettings = GetRBLValue(RBLShowListSettings);
            field.ShowInNewForm = GetRBLValue(RBLShowNew);
            field.ShowInVersionHistory = RBLShowVersionHistory.SelectedIndex == 0;
            field.ShowInViewForms = GetRBLValue(RBLShowViewForms);
            field.Update();

            BindFieldControls(field);
        }

        private void BindListDropDown()
        {
            DDLList.Items.Clear();
            DDLList.Items.Add(string.Empty);
            SPListCollection lists = SPContext.Current.Web.Lists;
            foreach (SPList list in lists)
                DDLList.Items.Add(new ListItem(list.Title, list.ID.ToString()));
        }

        private SPList GetList()
        {
            if (string.IsNullOrEmpty(DDLList.SelectedValue)) return null;
            SPList list = SPContext.Current.Web.Lists[new Guid(DDLList.SelectedValue)];
            return list;
        }

        private SPField GetField()
        {
            SPList list = GetList();
            if (list == null) return null;
            if (string.IsNullOrEmpty(DDLField.SelectedValue)) return null;
            SPField field = list.Fields[new Guid(DDLField.SelectedValue)];
            return field;
        }

        private void BindFieldControls(SPField field)
        {
            TxtTitle.Text = field.Title;
            TxtAuthInfo.Text = field.AuthoringInfo;
            MapRBLToNullableBool(RBLShowDisp, field.ShowInDisplayForm);
            MapRBLToNullableBool(RBLShowEdit, field.ShowInEditForm);
            MapRBLToNullableBool(RBLShowListSettings, field.ShowInListSettings);
            MapRBLToNullableBool(RBLShowNew, field.ShowInNewForm);
            RBLShowVersionHistory.SelectedIndex = field.ShowInVersionHistory ? 0 : 1;
            MapRBLToNullableBool(RBLShowViewForms, field.ShowInViewForms);
        }

        private void ResetFieldControls()
        {
            TxtAuthInfo.Text = TxtTitle.Text = string.Empty;
            RBLShowDisp.SelectedIndex = 2;
            RBLShowEdit.SelectedIndex = 2;
            RBLShowListSettings.SelectedIndex = 2;
            RBLShowNew.SelectedIndex = 2;
            RBLShowVersionHistory.SelectedIndex = 1;
            RBLShowViewForms.SelectedIndex = 2;
        }

        private void MapRBLToNullableBool(RadioButtonList rbl, Nullable<bool> value)
        {
            rbl.SelectedIndex = value.HasValue ? value.Value ? 0 : 1 : 2;
        }

        private Nullable<bool> GetRBLValue(RadioButtonList rbl)
        {
            if (rbl.SelectedIndex == 2) return null;
            return rbl.SelectedIndex == 0;
        }
    }
}
